<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>条件脚本管理</title>
    <#include "/pubHeadStyle.html">
</head>
<body>
<div class="bpmAuthorize" style="position: relative;height: 100%;">
    <div class="tablesize roles" style="position: absolute;top:0;right:0px;bottom: 0;left: 0px;padding:10px">
        <table id="conditionScript" lay-filter="conditionScript"></table>
    </div>
</div>
<script type="text/html" id="conditionScriptBar">
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
</script>
<script type="text/html" id="headerToolsBar">
    <div class="ccpui-enterSel">
        <div class="layui-btn-container">
            <a class="layui-btn layui-btn-sm" lay-event="query">
                <i class="layui-icon layui-icon-search"></i>
                <span>查询</span>
            </a>
            <a class="layui-btn layui-btn-sm" lay-event="add">
                <i class="layui-icon layui-icon-add-1"></i>
                <span>新增</span>
            </a>
            <a class="layui-btn layui-btn-sm" lay-event="edit">
                <i class="layui-icon layui-icon-edit"></i>
                <span>编辑</span>
            </a>
            <a class="layui-btn layui-btn-danger layui-btn-sm" lay-event="del">
                <i class="layui-icon layui-icon-delete"></i>
                <span>删除</span>
            </a>
            <a class="layui-btn layui-btn-sm" lay-event="export">
                <i class="layui-icon layui-icon-export"></i>
                <span>导出</span>
            </a>
            <a class="layui-btn layui-btn-sm" lay-event="import">
                <i class="layui-icon layui-icon-download-circle"></i>
                <span>导入</span>
            </a>
            <a class="layui-btn layui-btn-sm" lay-event="reset">
                <i class="layui-icon layui-icon-refresh"></i>
                <span>重置</span>
            </a>
        </div>
        <form class="layui-form" action="" lay-filter="queryForm" style="margin-top:10px">
            <div class="table-top-filter">
                <div class="table-filter-alone">
                    <label class="layui-form-label">脚本所在类名</label>
                    <input type="text" name="Q_className_SL" id="name" class="layui-input filter-value" placeholder="脚本所在类名"/>
                </div>
                <div class="table-filter-alone">
                    <label class="layui-form-label">方法名称</label>
                    <input type="text" name="Q_methodName_SL" id="name" class="layui-input filter-value" placeholder="方法名称"/>
                </div>
                <div class="table-filter-alone">
                    <label class="layui-form-label">是否有效</label>
                    <select type="text" name="Q_enable_L" id="status" class="layui-input filter-value">
                        <option value="">请选择</option>
                        <option value="0">是</option>
                        <option value="1">否</option>
                    </select>
                </div>
                <div class="table-filter-alone">
                    <label class="layui-form-label">方法描述</label>
                    <input type="text" name="Q_methodDesc_SL" id="name" class="layui-input filter-value" placeholder="方法描述"/>
                </div>
            </div>
        </form>
    </div>
</script>

<script>
    layui.config({
        base: serverPath.systemPath + staticPath.systemPath //静态资源所在路径
    }).extend({
        index: 'index' //主入口模块
    }).use(['index','table','commons','layer','setter','common','totalFilter','admin','form'],function(){
        var table = layui.table;
        var commons = layui.commons;
        var layer = layui.layer;
        var setter = layui.setter;
        var common = layui.common;
        var totalFilter = layui.totalFilter;
        var admin = layui.admin;
        var form = layui.form;
        var $ = layui.$;

        var tableH = commons.tabHeight();

        loadTable(serverPath.workflowPath + setter.workflowLinkPath + 'conditionScript/selector');

        function loadTable(url){
            table.render({
                elem: '#conditionScript'
                //, cellMinWidth: 80
                , title: '分管授权列表'
                , height: tableH
                , url: url
                // ,data:[{id:'1',username:'1',sex:'2'}]
                , method: 'post'
                ,defaultToolbar: ['filter', 'print']
                , toolbar: '#headerToolsBar'
                ,page: true //开启分页
                , cols: [[
                    {type: 'checkbox'}
                    , {field: 'id', title: 'id', align: 'center',hide:true}
                    , {field: 'methodName', title: '方法名称', align: 'center',templet: function(d){
                            var html = '';
                                html += '<a href="javascript:void(0);" onclick=onAuthActClick("'+ d.id +'") style="color: blue;margin-right: 5px;">'+ d.methodName +'</a>';
                            return html === '' ? '无' : html;
                        }}
                    , {field: 'methodDesc', title: '方法描述', align: 'center'}
                    , {field: 'className', title: '脚本所在类名', align: 'center'}
                    , {field: 'classInsName', title: '类实例名', align: 'center'}
                    , {field: 'enable', width: 100, title: '是否有效', align: 'center',templet: function(d){
                            if (d.enable == 0){
                                return '<span style="color: green">是</span>'
                            }else {
                                return '<span style="color: red">否</span>'
                            }
                        }}
                    , { title: '管理', toolbar: '#conditionScriptBar', width: 150, align: 'center'}
                ]],done:function () {
                    common.tableSelectCon('.ccpui-enterSel .layui-form input','a[lay-event=query]','.ccpui-enterSel');
                }
            });
            totalFilter.init('conditionScript');
        }

        // 新增或修改弹窗
        function addOrUpdateHandle(type,data){
            layer.open({
                type: 2,
                title: type === 'add' ? '新增' : '修改',
                area: ['60%', '560px'],
                content: serverPath.workflowPath + setter.workflowLinkPath + 'conditionScript/view/edit' + (type === 'add' ?  '' : '?id='+data),
                skin: 'btn-class',
                btn: ['保存','取消'],
                btnAlign: 'c',
                yes: function(index,layero){
                    var form = layero.find('iframe').contents().find(".layui-form");
                    var ConditionScript = {};
                    ConditionScript.id = form.find("input[name=id]").val();
                    ConditionScript.className = form.find("select[name=className]").val();
                    ConditionScript.classInsName = form.find("input[name=classInsName]").val();
                    ConditionScript.methodName = form.find("select[name=methodName]").val();
                    ConditionScript.methodDesc = form.find("input[name=methodDesc]").val();
                    ConditionScript.returnType = form.find("input[name=returnType]").val();
                    ConditionScript['dialog-type'] = form.find("select[name=dialog-type]").val();
                    ConditionScript['dialog-param'] = form.find("select[name=dialog-param]").val();
                    ConditionScript.enable = form.find("select[name=enable]").val();
                    $.ajax({
                        type: 'POST',
                        url: serverPath.workflowPath + setter.workflowLinkPath + 'conditionScript/getMethodsByName',
                        data: {name:form.find("select[name=className]").val()},
                        dataType:'json',
                        success: function(data){
                            if(data.data.length>0){
                                $.each(data.data, function(key, val) {
                                    if (form.find("select[name=methodName]").val()==val.methodName){
                                        ConditionScript.argument=val.para;
                                        ConditionScript.paraDesc=[];
                                        ConditionScript.paraCt=[];
                                        ConditionScript['dialog-type']=[];
                                        ConditionScript['dialog-param']=[];
                                        $.each(ConditionScript.argument,function (k,v) {
                                            if (ConditionScript.argument.length>1){
                                                ConditionScript.paraDesc.push(form.find("input[name=paraDesc]").eq(k).val());
                                                ConditionScript.paraCt.push(form.find("select[name=paraCt]").eq(k).val());
                                                ConditionScript['dialog-type'].push(form.find("select[name=dialog-type]").eq(k).val());
                                                ConditionScript['dialog-param'].push(form.find("select[name=dialog-param]").eq(k).val());
                                            }else {
                                                ConditionScript.paraDesc;
                                                ConditionScript.paraCt;
                                                ConditionScript['dialog-type']="";
                                                ConditionScript['dialog-param']="";
                                                ConditionScript.paraDesc=form.find("input[name=paraDesc]").eq(k).val();
                                                ConditionScript.paraCt=form.find("select[name=paraCt]").eq(k).val();
                                                ConditionScript['dialog-type']=form.find("select[name=dialog-type]").eq(k).val();
                                                ConditionScript['dialog-param']=form.find("select[name=dialog-param]").eq(k).val();
                                            }


                                            ConditionScript.argument[k].paraDesc=form.find("input[name=paraDesc]").eq(k).val();
                                            if (form.find("select[name=paraCt]").eq(k).val()==0){
                                                ConditionScript.argument[k].paraCt=form.find("select[name=paraCt]").eq(k).val();
                                                ConditionScript.argument[k].dialog=form.find("select[name=dialog-type]").eq(k).val();
                                                ConditionScript.argument[k].target=form.find("select[name=dialog-param]").eq(k).val();
                                            }else {
                                                ConditionScript.argument[k].paraCt=form.find("select[name=paraCt]").eq(k).val();
                                            }
                                        });
                                        $.ajax({
                                            url: serverPath.workflowPath + setter.workflowLinkPath + 'conditionScript/save',
                                            method: 'post',
                                            dataType: 'json',
                                            data:{
                                                id:ConditionScript.id,
                                                className:ConditionScript.className,
                                                classInsName:ConditionScript.classInsName,
                                                methodName:ConditionScript.methodName,
                                                methodDesc:ConditionScript.methodDesc,
                                                returnType:ConditionScript.returnType,
                                                argument:JSON.stringify(ConditionScript.argument),
                                                enable:ConditionScript.enable
                                            },
                                            success: function(data){
                                                if (data.code==0){
                                                    layer.close(index);
                                                    layer.msg(data.msg,{time: 2500, icon:6});
                                                    table.reload('conditionScript',{page: {curr: 1}});
                                                }else {
                                                    layer.msg(data.msg,{time: 2500, icon:5});
                                                }
                                            },
                                            error: function (XMLHttpRequest, textStatus, errorThrown) {
                                                layer.alert("保存失败！" + XMLHttpRequest.status);
                                            }
                                        });
                                    }
                                });
                            }else {
                                layer.msg("没有获取到有效的方法，请重新选择！",{time: 2500, icon:0});
                            }

                        }
                    });
                    return false;
                },
                btn2: function(index,layero){
                    layer.close(index);
                }
            });
        }
        //监听工具条
        table.on('tool(conditionScript)', function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的 DOM 对象（如果有的话）
            if(layEvent === 'edit'){ //编辑
                addOrUpdateHandle('edit',data.id);
                //do somehing
            } else if(layEvent === 'del'){ //删除
                delAjax(data.id.toString());
            }
        });
        // 监听头工具栏事件
        table.on('toolbar(conditionScript)',function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            var data = checkStatus.data; //获取选中行数据
            switch (obj.event) {
                case 'query':
                    common.tableConditionRender("conditionScript","queryForm",{where:{}},false);
                    break;
                case 'add':
                    addOrUpdateHandle('add');
                    break;
                case 'edit':
                    if(data.length !== 1){
                        layer.msg("请选择一项数据!", {time: 2500, icon:5});
                        return false;
                    }
                    addOrUpdateHandle('edit',data[0].id);
                    break;
                case 'del':
                    if(data.length === 0){
                        layer.msg("请选择一项数据!", {time: 2500, icon:5});
                        return false;
                    }
                    var ids = $.map(data,function(v){
                        return v.id;
                    });
                    layer.confirm("确认删除吗?",{icon:0},function(index){
                        layer.close(index);
                        $.ajax({
                            url: serverPath.workflowPath + setter.workflowLinkPath + 'conditionScript/del',
                            type: 'post',
                            dataType: 'json',
                            data: {
                                ids: ids.toString()
                            },
                            success: function(data){
                                if(data.code !== 0){
                                    layer.msg(data.msg, {time: 2500, icon:5});
                                    return false;
                                }
                                layer.msg(data.msg, {time: 500, icon:6});
                                table.reload('conditionScript',{page: {curr: 1}});
                            },
                            error: function (XMLHttpRequest, textStatus, errorThrown) {
                                layer.alert("删除失败！" + XMLHttpRequest.status);
                            }
                        });
                    });
                    break;
                case 'export':
                    if (data.length == 0) {
                        layer.confirm('还没有选择，是否导出全部？', {
                            btn: ['确定', '取消'] //按钮
                        }, function (index) {
                            layer.close(index);
                            var url = serverPath.workflowPath + setter.workflowLinkPath + 'conditionScript/exportXml';
                            window.location.href = url;
                        });
                    } else {
                        layer.confirm('确定导出吗？', {
                            btn: ['确定', '取消'] //按钮
                        }, function (index) {
                            layer.close(index);
                            var ids = [];
                            for (var i = 0; i < data.length; i++) {
                                ids.push(data[i].id);
                            }
                            var tableIds = ids.join(',');
                            var url = serverPath.workflowPath + setter.workflowLinkPath + 'conditionScript/exportXml?tableIds=' + tableIds;
                            window.location.href = url;
                        });
                    }
                    break;
                case 'import':
                    var iframeWin;
                    parent.layer.open({
                        type: 2,
                        title: "导入",
                        shade: 0.3,
                        area: ['400px', '300px'],
                        content: serverPath.workflowPath + setter.workflowLinkPath + "conditionScript/view/importXml",
                        success: function(layero, index){
                            iframeWin = parent.window[layero.find('iframe')[0]['name']];
                        },
                        end: function(){
                            if(iframeWin.layui.needReload!=undefined){
                                table.reload('conditionScript',{page: {curr: 1}});
                            }
                        }
                    });
                    break;
                case 'reset':
                    common.tableConditionRender("conditionScript","queryForm",{where:{}},true);
                    break;
            }
        });

        //删除
        function delAjax(idstr) {
            $.ajax({
                url: serverPath.workflowPath + setter.workflowLinkPath + 'conditionScript/del',
                type: 'post',
                dataType: 'json',
                data: {
                    ids: idstr
                },
                success: function(data){
                    if(data.code !== 0){
                        layer.msg(data.msg, {time: 2500, icon:5});
                        return false;
                    }
                    layer.msg(data.msg, {time: 500, icon:6});
                    table.reload('conditionScript',{page: {curr: 1}});
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    layer.alert("删除失败！" + XMLHttpRequest.status);
                }
            });
        }
        // 点击流程名称弹窗
        window.onAuthActClick = function(defKey){
            addOrUpdateHandle('edit',defKey);
        }
    });
</script>
<style>
    .layui-table-tool-self {
        display: none;
    }
    .bpmAuthorize .table-top-filter div input {
        border-radius: 4px;
        float: left;
        width: 170px;
    }
    .bpmAuthorize .table-top-filter label.layui-form-label {
        width: 114px !important;
        float: left;
    }
</style>
</body>
</html>